package com.bys.admin.system.service.impl;

import com.bys.admin.system.entity.SysMenu;
import com.bys.admin.system.vo.SysMenuVo;
import com.bys.constant.CommonConstant;
import com.bys.admin.system.mapper.SysMenuMapper;
import com.bys.admin.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author bys
 * @since 2023-06-26
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuVo> getUserMenuList() {
        return sysMenuMapper.selectUserMenuList(CommonConstant.MENU_DIR);
    }

    @Override
    public List<SysMenuVo> getUserMenuTree(List<SysMenuVo> sysMenuVoList) {
        List<SysMenuVo> userMenuList = this.getUserMenuList();
        return this.flattenSysMenuVo(userMenuList);
    }

    /**
     * 树结构转列表
     *
     * @param sysMenuVoList 树结构
     * @return 列表数据
     */
    public List<SysMenuVo> flattenSysMenuVo(List<SysMenuVo> sysMenuVoList) {
        List<SysMenuVo> resultList = new ArrayList<>();
        for (SysMenuVo orgStatistic : sysMenuVoList) {
            resultList.add(orgStatistic);
            if (orgStatistic.getChildren() != null && !orgStatistic.getChildren().isEmpty()) {
                resultList.addAll(flattenSysMenuVo(orgStatistic.getChildren()));
                orgStatistic.setChildren(null);
            }
        }
        return resultList;
    }
}
